package com.example.service.impl;

import com.example.domain.PageResult;
import com.example.domain.Post;
import com.example.dao.PostMapper;
import com.example.service.PostService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
//帖子的增删改查
@Service
public class PostServiceImpl implements PostService {

    @Autowired
    private PostMapper postMapper;
    //查询所有
    @Override
    public PageResult<Post> findAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Post> list = postMapper.findAllRaw();
        PageInfo<Post> pageInfo = new PageInfo<>(list);

        return new PageResult<>(
                pageInfo.getList(),
                (int) pageInfo.getTotal(),
                pageInfo.getPageNum(),
                pageInfo.getPageSize()
        );
    }
    //按条件查询
    @Override
    public PageResult<Post> findByPage(int pageNum, int pageSize, Integer categoryId, String keyword) {
        PageHelper.startPage(pageNum, pageSize);
        List<Post> list = postMapper.findByCategoryAndKeyword(categoryId, keyword);
        PageInfo<Post> pageInfo = new PageInfo<>(list);

        System.out.println("Search results count: " + list.size());

        return new PageResult<>(
                pageInfo.getList(),
                (int) pageInfo.getTotal(),
                pageInfo.getPageNum(),
                pageInfo.getPageSize()
        );
    }
    //查询所有
    @Override
    public List<Post> findAllRaw() {
        return postMapper.findAllRaw();
    }
    //添加
    @Override
    public void save(Post post) {
        postMapper.save(post);
    }
    //修改
    @Override
    public void update(Post post) {
        postMapper.update(post);
    }
    //删除
    @Override
    public void delete(Integer id) {
        postMapper.delete(id);
    }
    //查询
    @Override
    public Post findById(Integer id) {
        return postMapper.findById(id);
    }
    //修改状态
    @Override
    public void updateStatus(Integer id, String status) {
        postMapper.updateStatus(id, status);
    }
    //查询
    @Override
    public List<Post> findByUserId(Integer userId) {
        return postMapper.findByUserId(userId);
    }
    //查询
    @Override
    public List<Post> findAllForAdmin(String keyword, String status) {
        return postMapper.findAllForAdmin(keyword, status);
    }
}
